package com.laolang.shop.modules.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laolang.shop.modules.product.entity.ProductCategory;
import com.laolang.shop.modules.product.mapper.ProductCategoryMapper;
import com.laolang.shop.modules.product.pojo.category.admin.vo.ProductCategoryAdminTreeVo;
import com.laolang.shop.modules.product.service.ProductCategoryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author khlbat
 * @version 1.0
 * @date 2020/11/3 0:58
 */
@AllArgsConstructor
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    private final ProductCategoryMapper productCategoryMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(ProductCategory entity) {
        entity.setCreateTime(LocalDateTime.now());
        entity.setVersion(1);
        return super.save(entity);
    }

    @Override
    public List<ProductCategoryAdminTreeVo> listByParentId(Long parentId) {
        return productCategoryMapper.listByParentId(parentId);
    }

    @Override
    public PageInfo<ProductCategory> page(Integer page, Integer size, Wrapper<ProductCategory> queryWrapper) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(list(queryWrapper));
    }
}
